package cn.com.yiqi.wsc.service.lottery.impl;

import cn.com.yiqi.wsc.Ro.AwardVO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.lottery.LotteryActivityDao;
import cn.com.yiqi.wsc.dao.lottery.LotteryActivityMapper;
import cn.com.yiqi.wsc.dao.lottery.LotteryItemsDao;
import cn.com.yiqi.wsc.dao.lottery.LotteryRecordMapper;
import cn.com.yiqi.wsc.dao.lottery.criteria.LotteryActivityCriteria;
import cn.com.yiqi.wsc.entity.lottery.*;
import cn.com.yiqi.wsc.enumeration.CommonEnum;
import cn.com.yiqi.wsc.enumeration.LotteryActivityCanUsePoint;
import cn.com.yiqi.wsc.service.lottery.LotteryActivityService;
import cn.com.yiqi.wsc.service.lottery.enumerate.ActivityStatus;
import cn.com.yiqi.wsc.service.lottery.enumerate.LotteryRecordStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 何刚
 * @version V1.0
 * @Title: LotteryActivityServiceImpl
 * @Package cn.com.yiqi.wsc.service.lottery.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/3/21
 */
@Service
public class LotteryActivityServiceImpl implements LotteryActivityService {

    @Autowired
    private LotteryActivityDao lotteryActivityDao;

    @Autowired
    private LotteryItemsDao lotteryItemsDao;

    @Autowired
    private LotteryRecordMapper lotteryRecordMapper;

    @Autowired
    private LotteryActivityMapper lotteryActivityMapper;

    @Override
    public Result<PageInfo<LotteryActivity>> pageList(LotteryActivityCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());

        PageInfo<LotteryActivity> pageInfo = new PageInfo<>(lotteryActivityDao.pageList(criteria));

        result.setReturnValue(pageInfo);

        return result;
    }

    @Transactional
    @Override
    public Result<LotteryActivityDTO> insert(LotteryActivityDTO activity, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        if (LotteryActivityCanUsePoint.YES.equals(activity.getCanUsePoint())) {

            String pointBuy = activity.getPoint() + ":" + activity.getBuy();

            activity.setPointBuy(pointBuy);

        }

        //默认未开始
        activity.setStatus(ActivityStatus.notStart.getValue().byteValue());

        lotteryActivityDao.insert(activity);

        List<LotteryItemsDTO> itemsList = activity.getItemsList();

        if (itemsList != null && !itemsList.isEmpty()) {

            for (LotteryItems items : itemsList) {
                items.setInsertUserId(userId);
                items.setActivityId(activity.getId());
            }
        }

        lotteryItemsDao.batchInsert(itemsList);

        return result;
    }

    @Override
    public Result<LotteryRecord> insertRecord(LotteryRecord record, Integer userId) {


        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        record.setStatus(LotteryRecordStatus.hasget.getValue());
        record.setWin((byte) 1);
        record.setJoinTime(new Date());
        lotteryRecordMapper.insertSelective(record);

        return result;
    }

    @Transactional
    @Override
    public Result update(LotteryActivityDTO activity, Integer userId) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        if (activity.getCanUsePoint() == 0) {
            activity.setPointBuy("");
        }

        if (activity.getJoinTimeType() != 5) {
            activity.setJoinTime(0);
        }

        lotteryActivityDao.update(activity);

        Integer activityId = activity.getId();

        //标记删除
//        lotteryItemsDao.deleteBatch(activityId);

        List<LotteryItemsDTO> itemsList = activity.getItemsList();

        if (itemsList != null && !itemsList.isEmpty()) {

            for (LotteryItems items : itemsList) {
                lotteryItemsDao.update(items);
//                items.setInsertUserId(userId);
//                items.setActivityId(activity.getId());
            }
        }

//        lotteryItemsDao.batchInsert(itemsList);


        return result;
    }

    @Override
    public Result delete(LotteryActivityCriteria criteria) {

        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());


        LotteryActivity lotteryActivity = new LotteryActivity();
        lotteryActivity.setId(criteria.getId());
        lotteryActivity.setDelete(CommonEnum.DELETED.getValue());

        lotteryActivityDao.update(lotteryActivity);

        return result;
    }

    @Override
    public Result enable(LotteryActivityCriteria criteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());


        LotteryActivity lotteryActivity = new LotteryActivity();
        lotteryActivity.setId(criteria.getId());
        lotteryActivity.setEnable(CommonEnum.ENABLE.getValue());

        lotteryActivityDao.update(lotteryActivity);

        return result;
    }

    @Override
    public Result disable(LotteryActivityCriteria criteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());


        LotteryActivity lotteryActivity = new LotteryActivity();
        lotteryActivity.setId(criteria.getId());
        lotteryActivity.setEnable(CommonEnum.DISABLE.getValue());

        lotteryActivityDao.update(lotteryActivity);
        return result;
    }

    @Override
    public Result<LotteryActivityDTO> detail(LotteryActivityCriteria criteria) {


        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());

        LotteryActivityDTO lotteryActivityDTO = lotteryActivityDao.detail(criteria);

        if (lotteryActivityDTO != null) {
            List<LotteryItemsDTO> lotteryItemsDTOs = lotteryItemsDao.getItemByActivityId(lotteryActivityDTO.getId());
            lotteryActivityDTO.setItemsList(lotteryItemsDTOs);
        }

        result.setReturnValue(lotteryActivityDTO);


        return result;
    }

    @Override
    public Result<PageInfo<AwardVO>> selectLotteryRecordByActivityId(LotteryActivityCriteria criteria) {


        Result<PageInfo<AwardVO>> result = new Result<>();
        result.setStatus(ResultStatusEnum.操作成功.getCode());


        PageHelper.startPage(criteria.getCurrentPage(), criteria.getPageSize());

        PageHelper.orderBy("mlr.d_join_time DESC");


        PageInfo<AwardVO> info = new PageInfo<>(lotteryRecordMapper.selectLotteryRecordByActivityId(criteria.getId()));

        result.setReturnValue(info);

        return result;
    }

    @Override
    @Transactional
    public Result refreshStatusByNowTime() {
        lotteryActivityMapper.updateStatusByStartTimeGTNow();
        lotteryActivityMapper.updateStatusByEndTimeLTNow();

        Result result = new Result<>();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        return result;

    }

    @Override
    public Result deleteRecord(Long id) {
        Result result = new Result();
        if (lotteryRecordMapper.deleteByPrimaryKey(id.intValue()) > 0) {
            result.setStatus(ResultStatusEnum.操作成功.getCode());
        } else {
            result.setStatus(ResultStatusEnum.服务器内部错误.getCode());
            result.setErrMsg("系统错误");
        }
        return result;
    }
}
